package com.bong.panpan.bongcharttools.custom;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by panpan on 2017/10/26.
 */

public class HeartRateRecord implements Serializable {

    public HashMap<String, int[]> heartData = null;

    public static boolean save(HeartRateRecord record, String path) {
        ObjectOutputStream outputStream = null;
        try {
            outputStream = new ObjectOutputStream(new FileOutputStream(path));
            outputStream.writeObject(record);
            outputStream.close();
            saveToCSV(record, path.replace(Constant.heartEndName,".csv"));
            return true;
        } catch (Exception e) {
            try {
                if (outputStream != null)
                    outputStream.close();
            } catch (Exception e1) {

            }
        }
        return false;
    }

    private static boolean saveToCSV(HeartRateRecord record, String path) {
        BufferedWriter outputStream = null;
        try {
            outputStream = new BufferedWriter(new FileWriter(path));
            ArrayList<String> name = new ArrayList<>();
            ArrayList<int[]> values = new ArrayList<>();
            for (Map.Entry<String, int[]> item :
                    record.heartData.entrySet()) {
                name.add(item.getKey());
                values.add(item.getValue());
            }
            StringBuffer stringBuffer = new StringBuffer();
            for (int i = 0; i < name.size(); i++) {
                if (i != 0)
                    stringBuffer.append(",");
                stringBuffer.append(name.get(i));
            }
            outputStream.write(stringBuffer.toString());
            outputStream.newLine();
            int index[] = new int[name.size()];
            for (int i = 0; i < index.length; i++) {
                index[i] = 0;
            }
            boolean have=false;
            for (int i = 0; ; i++) {
                stringBuffer.delete(0, stringBuffer.length());
                have = false;
                for (int j = 0; j < index.length; j++) {

                    if (j != 0)
                        stringBuffer.append(",");
                    if (i == values.get(j)[index[j]]) {
                        stringBuffer.append("" + values.get(j)[index[j] + 1]);
                        index[j] += 2;
                        have = true;
                    }
                }
                if (!have) {
                    break;
                } else {
                    outputStream.write(stringBuffer.toString());
                    outputStream.newLine();
                }
            }
            outputStream.close();
            return true;
        } catch (Exception e) {
            try {
                if (outputStream != null)
                    outputStream.close();
            } catch (Exception e1) {

            }
        }
        return false;
    }

    public static HeartRateRecord load(String path) {
        HeartRateRecord record = null;
        ObjectInputStream inputStream = null;
        try {

            inputStream = new ObjectInputStream(new FileInputStream(path));
            record = (HeartRateRecord) inputStream.readObject();
            inputStream.close();
        } catch (Exception e) {
            try {
                if (inputStream != null)
                    inputStream.close();
            } catch (Exception e1) {

            }
        }
        return record;
    }
}
